package search.impl;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GetHtmlAsync extends GetAll implements Runnable
{
	public List<String> getHtmls() {
		return htmls;
	}

	private List<String> htmls;
	private String url;

	public String getUrl()
	{
		return url;
	}

	GetHtmlAsync(List<String> htmls, String url)
	{
		this.htmls = htmls;
		this.url = url;
	}

	public void run()
	{
		getAllHtmls();
	}

	public void getAllHtmls() {
		try
		{
			Document departmentDoc = Jsoup.parse(getAllHtml(url));
			Elements elementsByClass = departmentDoc.getElementsByClass("department-list");
			for (Element overviewEntry : elementsByClass)
			{
				for (Element overview : overviewEntry.getElementsByTag("a"))
				{
					htmls.add(getAllHtml(overview.attr("href")));
				}
			}
		}
		catch (FileNotFoundException ignored)
		{
			ignored.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}

